package com.ejuornal.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.ejuornal.bean.EditorCommit;
import com.ejuornal.bean.Page;
import com.ejuornal.dao.impl.EditorCommitDaoImpl;
import com.ejuornal.service.EditorCommitService;

@Component("editorCommitService")
public class EditorCommitServiceImpl implements EditorCommitService {
	private EditorCommitDaoImpl editorCommitDao;

	public void add(EditorCommit editorCommit) {
		String sql = "INSERT INTO t_editor_commit VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)";
		editorCommitDao.add(
				sql,
				new Object[] { null, editorCommit.getPeriodicalId(), editorCommit.getUserId(),
						editorCommit.getIntroduce(), editorCommit.getReason(),
						editorCommit.getApplyDate(), null, null,
						editorCommit.getResult() });
	}

	public void update(EditorCommit editorCommit) {
		String sql = "UPDATE t_editor_commit SET checkedReason = ?, checkedDate = ?, result = ? WHERE editorCommitId = ?";
		editorCommitDao
				.update(sql,
						new Object[] { editorCommit.getCheckedReason(),
								new Date(),
								editorCommit.getResult(),
								editorCommit.getEditorCommitId() 
								});
	}

	public EditorCommit findById(int userId) {
		return editorCommitDao.findById(userId);
	}

	public List<EditorCommit> findByPeriodicalId(int periodicalId) {
		return editorCommitDao.findByPeriodicalId(periodicalId);
	}

	public Page<EditorCommit> findByProperty(String prop, Object value) {
		return editorCommitDao.findByProperty(prop, value);
	}

	@Override
	public List<EditorCommit> getEditorCommits(int articalId) {
		String  sql= "SELECT  ecommit.* FROM  t_artical artical,t_editor_commit ecommit , t_periodical  per,t_per_periodical perper WHERE  "+
				"artical.perPeriodicalId =perper.perPeriodicalId AND perper.periodicalId = per.periodicalId AND per.periodicalId = ecommit.periodicalId "+ 
				"  AND  artical.articalId = ? ";
		
		return editorCommitDao.getEditorCommits(sql, articalId);
	}

	@Override
	public boolean isAppliedCommit(int userId) {
		return editorCommitDao.isAppliedCommit(userId);
	}
	
	public EditorCommitDaoImpl getEditorCommitDao() {
		return editorCommitDao;
	}

	@Resource(name = "editorCommitDao")
	public void setEditorCommitDao(EditorCommitDaoImpl editorCommitDao) {
		this.editorCommitDao = editorCommitDao;
	}
}
